package com.github.thinwonton.activemq.showcase.sample.durabletopic;

import com.github.thinwonton.activemq.showcase.sample.utils.ActiveMQManager;

import javax.jms.*;

public class Producer {
    public static final String TOPIC_NAME = "test-durable-topic";

    public static void main(String[] args) {
        System.out.println("Producer started!");

        //构建非持久消息和持久消息的内容
        String message_persistent = "持久的消息 : " + System.currentTimeMillis();
        String message_non_persistent = "非持久的消息 : " + System.currentTimeMillis();

        try {
            Connection connection = ActiveMQManager.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            Topic topic = session.createTopic(TOPIC_NAME);
            MessageProducer producer = session.createProducer(topic);


            //发送一个非持久信息
            TextMessage non_persistent_message = session.createTextMessage(message_non_persistent);
            non_persistent_message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            producer.send(non_persistent_message);

            //发送一个持久信息
            TextMessage persistent_message = session.createTextMessage(message_persistent);
            persistent_message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            producer.send(persistent_message);

            System.out.println("成功发送消息：" + non_persistent_message.getText());
            System.out.println("成功发送消息：" + persistent_message.getText());

            producer.close();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Producer end!");
    }
}
